package com.china.bike.controller;

import com.alibaba.fastjson.JSON;
import com.china.bike.dto.BikeInfoVo;
import com.china.bike.dto.BikeListParam;
import com.china.bike.dto.PageBean;
import com.china.bike.dto.ResultBean;
import com.china.bike.entity.BikeInfo;
import com.china.bike.service.BikeInfoService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/bike-info")
public class BikeInfoController {

    @Autowired
    private BikeInfoService bikeInfoService;

    @RequestMapping(value = "/save-bike", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean saveBikeInfo(@RequestParam(value = "bikeCount") Integer bikeCount) {
        int count = bikeInfoService.saveBikeInfo(bikeCount);
        ResultBean rlt = new ResultBean();
        if (count >= 1) {
            rlt.setResult(ResultBean.RESULT_SUCCESS);
            rlt.setMsg("添加" + count + "辆自行车成功!");
        } else {
            rlt.setResult(ResultBean.RESULT_FAILED);
            rlt.setMsg("添加失败!");
        }
        return rlt;
    }

    @RequestMapping(value = "/list-bikes")
    @ResponseBody
    public String listBikes(@RequestParam(value = "start", required = false) Integer start,
                            @RequestParam(value = "page", required = false) Integer page,
                            @RequestParam(value = "limit", required = false) Integer limit,
                            @RequestParam(value = "bikeStatusCode", required = false) Integer bikeStatusCode,
                            @RequestParam(value = "bikeNum", required = false) String bikeNum,
                            @RequestParam(value = "storageDate", required = false) String storageDate) {
        BikeListParam bikeListParam = new BikeListParam();
        bikeListParam.setStart(start);
        bikeListParam.setPage(page);
        bikeListParam.setLimit(limit);
        if (bikeStatusCode != null) {
            bikeListParam.setStatus(Byte.valueOf(String.valueOf(bikeStatusCode)));
        }
        bikeListParam.setBikeNum(bikeNum);
        bikeListParam.setStorageDate(storageDate);
        PageHelper.startPage(start, limit);
        List<BikeInfoVo> list = bikeInfoService.listBikes(bikeListParam);
        int count = bikeInfoService.listBikeCount(bikeListParam);//总记录数
        PageBean<BikeInfoVo> pageData = new PageBean<>(start, limit, count);
        pageData.setRows(list);
        return JSON.toJSONString(pageData);
    }

    @RequestMapping(value = "/delete-bike")
    public String deleteBike(@RequestParam(value = "bikeNum") String bikeNum) {
        bikeInfoService.deleteBike(bikeNum);
        return "/page/bike/itemMgnt";
    }


    @RequestMapping(value = "/update-bike-status", produces = "text/html;charset=UTF-8")
    public String updateBikeStatus(@RequestParam(value = "bikeStatus") Integer bikeStatus,
                                   @RequestParam(value = "bikeNum") String bikeNum) {
        BikeInfo bikeInfo = new BikeInfo();
        bikeInfo.setBikeNum(bikeNum);
        bikeInfo.setStatus(Byte.valueOf(String.valueOf(bikeStatus)));
        int row = bikeInfoService.updateBikeStatus(bikeInfo);
        ResultBean rlt = new ResultBean();
        if (row >= 1) {
            rlt.setResult(ResultBean.RESULT_SUCCESS);
            rlt.setMsg("更新成功!");
        } else {
            rlt.setResult(ResultBean.RESULT_FAILED);
            rlt.setMsg("更新失败!");
        }
        return "/page/bike/itemMgnt";
    }

}
